//
//  MHSearchMainViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/29.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHSearchMainViewController.h"
#import <AFNetworking.h>
#import "MHComicIntroduct.h"
#import "MHSearchShowViewController.h"
#import "MHSearchView.h"


#define kListUrl @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx"


@interface MHSearchMainViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *listArr;
@property (nonatomic, strong) MHSearchView *searchView;

@end

@implementation MHSearchMainViewController




- (void)requestSearchListDataWithString:(NSString *)string
{
//    ?key=%E6%96%97%E7%BD%97%E5%A4%A7%E9%99%86&method=getkey
    NSString *name = [NSString stringWithCString:[string UTF8String] encoding:NSUTF8StringEncoding];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[name,@"getkey"] forKeys:@[@"key",@"method"]];
    [[AFHTTPRequestOperationManager manager] GET:kListUrl parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        self.listArr = responseObject;
         [self.tableView reloadData];
       
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}






- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addSearchBar];
    
    
    _searchView = [[MHSearchView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_searchView];
    
    
    for (int i = 0; i < 8 ; i++) {
        [((UIButton *)_searchView.subviews[i + 2]) addTarget:self action:@selector(didClickSearch:) forControlEvents:UIControlEventTouchUpInside];
    }
    

    
    
    
    
    
}

// 红色btn点击事件
- (void)didClickSearch:(UIButton *)sender
{
    MHSearchShowViewController *showVC = [[MHSearchShowViewController alloc]init];
    showVC.name = sender.titleLabel.text;
    [self.navigationController pushViewController:showVC animated:YES];
}


- (void)addSearchBar
{
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, Width * 0.7, 30)];
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.searchBar.placeholder = @"请输入要搜索的书名或作者";
    self.searchBar.delegate = self;
    self.navigationItem.titleView = self.searchBar;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    
    [searchBar setShowsCancelButton:NO animated:YES];// 取消按钮回收
    [searchBar resignFirstResponder]; // 取消第一响应值,键盘回收,搜索结束
}

// 点击搜索的时候
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    MHSearchShowViewController *showVC = [[MHSearchShowViewController alloc]init];
    showVC.name = searchBar.text;
    [self.navigationController pushViewController:showVC animated:YES];

    
}

#warning UISearchBar常用的几种代理方法


// 开始输入文本
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    
    NSLog(@"开始输入文本");
    // 开始输入文本时显示删除键
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height- 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:@"cell"];
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    [searchBar setShowsCancelButton:YES animated:YES];
}



// 搜索内容改变的时候 在这个方法里面实现实时显示结果
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
{
    NSLog(@"textDidChange:%@", searchText);
    self.listArr = [NSArray array];
    if (searchText.length > 0) {
         [self requestSearchListDataWithString:searchText];
    }
   

    // 刷新数据
    [self.tableView reloadData];
    
    
}


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [_tableView removeFromSuperview];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = self.listArr[indexPath.row];
    return cell;
}

// 点击tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.searchBar resignFirstResponder];
    MHSearchShowViewController *showVC = [[MHSearchShowViewController alloc]init];
    showVC.name = self.listArr[indexPath.row];
    [self.navigationController pushViewController:showVC animated:YES];
}



@end
